using UnityEngine;
using UnityEngine.Events;

public class Body : MonoBehaviour
{
	public Zombie zombie;

	public float health = 100f;

	public float speed = 1f;

	public AudioSource audioSource;

	public GameObject[] destroyWhenDead;

	public ParticleSystem vomitPs;

	public AudioClip[] voiceAudios;

	public AudioClip[] bitAudios;

	public AudioClip[] hurtAudios;

	public GameObject bitEffect;

	public GameObject mouth;

	public GameObject chest;

	public ParticleSystem bodyExplosionPs;

	private AudioClip voiceAudio;

	public UnityEvent onDie;

	[HideInInspector]
	public bool isCrawl;

	[HideInInspector]
	public bool isDead;

	[HideInInspector]
	public BodyPart[] bodyParts;

	private int groundLayer;

	private void Awake()
	{
		bodyParts = GetComponentsInChildren<BodyPart>();
		zombie = base.transform.GetComponentInParent<Zombie>();
		BodyPart[] array = bodyParts;
		for (int i = 0; i < array.Length; i++)
		{
			array[i].body = this;
		}
		if (voiceAudios.Length != 0)
		{
			voiceAudio = voiceAudios[Random.Range(0, voiceAudios.Length)];
		}
	}

	private void Start()
	{
		Invoke("Voice", Random.Range(0f, 2f));
	}

	public void Voice()
	{
		if (!isDead && (bool)voiceAudio)
		{
			Speak(voiceAudio);
			Invoke("Voice", voiceAudio.length + Random.Range(0f, 3f));
		}
	}

	public void Speak(AudioClip audio)
	{
		if (!isDead)
		{
			audioSource.clip = audio;
			audioSource.Play();
		}
	}

	public void Die()
	{
		if (isDead)
		{
			return;
		}
		health = 0f;
		isDead = true;
		if (destroyWhenDead.Length != 0)
		{
			GameObject[] array = destroyWhenDead;
			for (int i = 0; i < array.Length; i++)
			{
				Object.Destroy(array[i]);
			}
		}
		DaymicBody();
		Object.Destroy(audioSource, 0.5f);
		Staticlize();
		if ((bool)zombie)
		{
			zombie.Die();
		}
		if ((bool)vomitPs)
		{
			vomitPs.Stop();
			Object.Destroy(vomitPs, 3f);
		}
		onDie.Invoke();
	}

	public void Staticlize()
	{
		BodyPart[] array = bodyParts;
		foreach (BodyPart bodyPart in array)
		{
			if ((bool)bodyPart)
			{
				bodyPart.Staticlize();
			}
		}
		Object.Destroy(this);
		Object.Destroy(GetComponent<Rigidbody2D>());
		Object.Destroy(GetComponent<Collider2D>());
		Object.Destroy(GetComponent<Animator>());
	}

	public void DaymicBody()
	{
		BodyPart[] array = bodyParts;
		foreach (BodyPart bodyPart in array)
		{
			if ((bool)bodyPart && (bool)bodyPart.rb)
			{
				bodyPart.rb.isKinematic = false;
			}
		}
	}

	public void KinematicBody()
	{
		BodyPart[] array = bodyParts;
		foreach (BodyPart bodyPart in array)
		{
			if ((bool)bodyPart && (bool)bodyPart.rb)
			{
				bodyPart.rb.isKinematic = true;
			}
		}
	}

	public void AnimatonBody()
	{
		BodyPart[] array = bodyParts;
		foreach (BodyPart bodyPart in array)
		{
			if ((bool)bodyPart)
			{
				bodyPart.rb.isKinematic = true;
			}
		}
	}

	public void SetColliderLayer(int layer)
	{
		BodyPart[] array = bodyParts;
		foreach (BodyPart bodyPart in array)
		{
			if (isDead)
			{
				break;
			}
			if ((bool)bodyPart)
			{
				bodyPart.gameObject.layer = layer + Ground.groundTotal;
			}
		}
		if ((bool)vomitPs)
		{
			ParticleSystem.CollisionModule collision = vomitPs.collision;
			collision.collidesWith = (1 << layer) | 0x800;
		}
	}

	public void OnHit(BodyPart bodyPart, Weapon weapon, Vector3 direction, Vector2 contactPoint)
	{
		if (!(weapon == null))
		{
			if ((bool)zombie)
			{
				zombie.OnHit(bodyPart, weapon, direction, contactPoint);
			}
			float num = weapon.damage * bodyPart.damageRatio;
			bodyPart.damageCount += num;
			health -= num;
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (collision.gameObject.tag == "Ground" && base.gameObject.tag != "Body")
		{
			SetColliderLayer(collision.gameObject.layer);
		}
	}
}
